package system

import base.Dimensions

/* 机构树*/

class Department {
    /* 机构名称*/
    String name
    /* 机构编码*/
    String code
    /* 机构类型*/
    DepartmentType departmentType
    /*备注*/
    String remark
/*删除标记 0 启用 / 1 停用*/
    String dr = '0'
    /*创建人*/
    StaffMember creater

    //  todo
    /*费用属性  JC_002*/
    Dimensions fyAttribute
    /*核算名称*/
    String hsName
    /*核算编码*/
    String hsCode

    //  部门类型为公司的 需要填写  纳税人识别号  地址及电话  开户行及账号
/*纳税人识别号*/
    String taxpayerNum
    /*地址及电话*/
    String address
    /*开户行及账号*/
    String bank

    static belongsTo = [parent: Department]
    static hasMany = [children: Department]

    Date lastUpdated
    static constraints = {
        name blank: false
        code unique: true, validator: { val, obj ->
            if (obj.parent == null) return true
            if (!val.startsWith(obj.parent.code))
                return ["invalid.prefix", val, obj.parent.code]
            else
                return true
        }
        parent nullable: true
        remark nullable: true, maxSize: 4000
        departmentType nullable: true
        dr nullable: true
        creater nullable: true
        lastUpdated nullable: true
        fyAttribute nullable: true
        hsName nullable: true
        hsCode nullable: true
        taxpayerNum nullable: true
        address nullable: true
        bank nullable: true
    }

    String toString() {
        "${name}"
    }

    static mapping = {
        sort "code"
    }


    String getText() {
        return name
    }

    transient utilsService

    def buildDomain() {
        def b = [:] + this.properties
        b.remove('transients')
        b.remove('typelist')
        b.remove('showCreateMethodMap')
        b = utilsService.truncProp(this, b) { amap ->
            amap << ['id': this.id]

        }
        b
    }

    def buildDomainList(list) {
        def rds = []
        list.each {
            def b = [:] + it.properties
//            b.remove('plate')
            def that = it
            b = utilsService.truncProp(that, b) { amap ->
                amap << ['id': it.id]
                amap << ['parentCode': it.parent?.code ?: ""]
                amap << ['departmentTypeName': it.departmentType?.name ?: ""]
            }
            rds << b
        }
        return rds
    }
    def includechildlist = {

        def results = Department.findAllByCodeLike(this.code + '%')
        def rds = []
        results.each {
            def b = [:] + it.properties
            def that = it
            b = utilsService.truncProp(that, b) { amap ->
                amap << ['id': it.id]
            }
            rds << b
        }
        return rds

    }
}
